package com.ruge.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruge.mapper.DataBaseMapper;
import com.ruge.mapper.RugeGenPackageNameMapper;
import com.ruge.model.RugeGenPackageName;
import com.ruge.service.GenCodeService;
import freemarker.template.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GenCodeServiceImpl implements GenCodeService {


    @Resource
    private DataBaseMapper dataBaseMapper;

    @Resource
    private Configuration configuration;

    @Resource
    private RugeGenPackageNameMapper rugeGenPackageNameMapper;

    /**
     * 代码生成器
     */
    @Override
    public Map<String, Object> generate(Map<String, String> params) {
        JSONObject tableInfo;
        List<JSONObject> columnInfos;
        Map<String, Object> result = new HashMap<>();
        try {
            tableInfo = dataBaseMapper.getTableInfo(params.get("tableName").trim());
            columnInfos = dataBaseMapper.getColumnInfo(params.get("tableName").trim());
            columnInfos.forEach(column -> {
                String columnName = column.getString("fieldName");
                columnName = columnName.toLowerCase();
                columnName= StrUtil.toCamelCase(columnName);
                columnName = secondCharToLower(columnName);
                column.put("propertyName", columnName);
            });
            RugeGenPackageName rugeGenPackageName = rugeGenPackageNameMapper.selectList(null).get(0);
            Map<String, Object> map = new HashMap<>();
            map.put("packageName", rugeGenPackageName);
            map.put("tableInfo", tableInfo);
            map.put("columnInfos", columnInfos);
            //填充模板
            result.put("model", FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate("ftl/Model.ftlh"), map));
            result.put("mapper", FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate("ftl/Mapper.ftlh"), map));
            result.put("mapperXml", FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate("ftl/Mapper.xml.ftlh"), map));
            result.put("service", FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate("ftl/Service.ftlh"), map));
            result.put("serviceImpl", FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate("ftl/ServiceImpl.ftlh"), map));
            result.put("controller", FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate("ftl/Controller.ftlh"), map));

            result.put("packageName", rugeGenPackageName);
            result.put("tableInfo", tableInfo);
            result.put("columnInfos", columnInfos);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        result.put("msg", "查询成功");
        return result;
    }


    public static String secondCharToLower(String input) {
        return input == null || input.length() < 2 ? input :
                input.charAt(0) +
                        String.valueOf(Character.toLowerCase(input.charAt(1))) +
                        input.substring(2);
    }
}
